/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.controller;

import java.util.ArrayList;
import java.util.List;

import uk.org.aspellclark.agenedb.gedcom.model.Family;
import uk.org.aspellclark.agenedb.gedcom.model.FamilyChild;
import uk.org.aspellclark.agenedb.gedcom.model.Individual;

import com.db4o.ObjectSet;
import com.db4o.foundation.NotImplementedException;

public class IndividualController extends AbstractDB4OController {

	private String currentPrsnId;

	/**
	 * Constructor
	 * @param dbCntrlr
	 */
	public IndividualController(Db4oController dbCntrlr) {
		super(dbCntrlr);
	}

	/**
	 * @param ind
	 */
	public void addIndividual(Individual ind) {
		this.dbCntrlr.db.store(ind);
	}

	/**
	 * @return the currentPrsnId
	 */
	public final String getCurrentPrsnId() {
		return this.currentPrsnId;
	}

	public Individual getIndividual(final String xref) {
		Individual ind = new Individual();
		ind.xref = xref;
		ObjectSet<Individual> retrndObjs = this.dbCntrlr.db.queryByExample(ind);

		if (retrndObjs.size() == 1) {
			ind = retrndObjs.next();
		} else {
			ind = null;
		}
		return ind;
	}

	public List<Individual> getIndividuals() {
		//		Individual ind = new Individual();
		//		ObjectSet retrndObjs = this.dbCntrlr.db.queryByExample(ind);
		List<Individual> objList = this.dbCntrlr.db.queryByExample(Individual.class);
		return objList;
	}

	public ArrayList<Individual> getSiblings(String xref) {
		ArrayList<Individual> objList = new ArrayList<Individual>();
		Individual ind = this.getIndividual(xref);

		List<FamilyChild> famList = ind.familiesWhereChild;
		if (famList.size() > 0) {
			Family fam = famList.get(0).family;

			for (String chldXref : fam.children) {
				objList.add(this.getIndividual(chldXref));
			}
		}
		return objList;
	}

	public Individual getSpouse() {
		throw new NotImplementedException();
	}

	/**
	 * @param currentPrsnId the currentPrsnId to set
	 */
	public final void setCurrentPrsnId(String currentPrsnId) {
		this.currentPrsnId = currentPrsnId;
	}

}
